#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Handler for static layers."""

__author__ = 'jasonstredwick@google.com (Jason Stredwick)'


# Disable 'Import not at top of file' lint error.
# pylint: disable-msg=C6204
try:
  import auto_import_fixer
except ImportError:
  pass  # This will fail on unittest, ok to pass.

from layer.models import layer_static

# Disable 'Import not at top of file' lint error.
# pylint: disable-msg=C6204
try:
  from google.appengine.ext import webapp
  from google.appengine.ext.webapp.util import run_wsgi_app
except ImportError:
  from google.appengine.ext import webapp
  from google.appengine.ext.webapp.util import run_wsgi_app


class Add(webapp.RequestHandler):
  """Handles the addition of a static layer."""

  def get(self):
    """Handles a get request, redirects to the post request."""
    self.post()

  def post(self):
    """Handles a post request.

    Returns the layer manifest for the specified simple layer.
    """
    # TODO(jasonstredwick): Convert to using a protocolo buffer and a series
    # of error codes for communicating between client and server.
    output = 'Already had url'

    url = self.request.get('url')
    if url and not layer_static.Get(url):
      layer_static.Create(url)
      output = 'Success'

    self.response.out.write(output)


def main():
  application = webapp.WSGIApplication([('/layer/static/add', Add)],
                                       debug=True)
  run_wsgi_app(application)

  return


if __name__ == '__main__':
  main()

